package com.zj.common.util;

import java.lang.reflect.Field;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringMyUtil {

	public static String getNohtml(String a) {
		String regex = "<[^>]*>";

		String b = a.replaceAll(regex, "").replaceAll("&ldquo;", "").replaceAll("&nbsp;", "").replaceAll("&rdquo;", "")
				.replaceAll("&quot;", "");
		return b;
	}

	public static boolean isNotEmpty(String a) {
		if (a == null) {
			return false;
		} else {

			if (a.equals("")) {
				return false;
			} else {
				return true;
			}

		}

	}

	public static boolean isEmpty(String a) {
		if (a == null) {
			return true;
		} else {

			if (a.equals("")) {
				return true;
			} else {
				return false;
			}

		}

	}

	public static <T> String isEmpty(T t, String[] strs) {
		Class clazz = t.getClass();
		try {
			for (String str : strs) {
				Field field = clazz.getDeclaredField(str);
				field.setAccessible(true);
				Object c = field.get(t);
				if (field.getType().getName().equals("java.lang.String")) {
					if (isEmpty((String) c)) {
						return str + "为空";
					}
				} else {
					if (null == c) {
						return str + "为空";
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			return "异常";
		}
		return null;
	}

	public static <T> String isEmptyField(T t, String[] fields) {
		Class clazz = t.getClass();
		try {
			for (String str : fields) {
				String[] split = str.split("-");
				Field field = clazz.getDeclaredField(split[0]);
				field.setAccessible(true);
				Object c = field.get(t);
				if (field.getType().getName().equals("java.lang.String")) {
					if (isEmpty((String) c)) {
						return split[1] + "为空";
					}
				} else {
					if (null == c) {
						return split[1] + "为空";
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			return "异常";
		}
		return null;
	}


	public static String dateSplit(String a) {
		return a.substring(0, 19);
	}

	public static String YearSplit(String a) {
		if (StringMyUtil.isNotEmpty(a)) {
			if (a.length() <= 10) {
				return a;
			}
			return a.substring(0, 10);
		} else {
			return null;
		}
		
	}

	public static String dateTimeSplit(String a) {
		if (StringMyUtil.isNotEmpty(a)) {
			if (a.length() <= 19) {
				return a;
			}
			return StringMyUtil.dateSplit(a);
		} else {
			return null;
		}
	}

	

	public static String getOrderIdByUUId() {
		int hashCodev = UUID.randomUUID().toString().hashCode();
		if (hashCodev < 0) {
			hashCodev = -hashCodev;
		}
		return String.format("%09d", hashCodev);
	}

	// 判断是不是中文
	public static boolean isContainChinese(String str) {

		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
		Matcher m = p.matcher(str);
		if (m.find()) {
			return true;
		}
		return false;
	}

	public static String getNubmer(int i) {
		if (i < 9) {
			return "00" + i;
		}
		if (9 < i && i < 99) {
			return "0" + i;
		}
		return i + "";
	}
}
